import { lazy } from "react";
import { createHashRouter } from "react-router";
import { createBrowserRouter, Navigate, type RouteObject } from "react-router";

const HeaderLayout = lazy(() => import("@/layout/HeaderLayout"));
const AsideLayout = lazy(() => import("@/layout/AsideLayout"));
const DashboardLayout = lazy(() => import("@/layout/DashboardLayout"));
const SettingLayout = lazy(() => import("@/layout/SettingLayout"));
const Dashboard = lazy(() => import("@/views/Dashboard"));
const Overview = lazy(() => import("@/views/Dashboard/Overview"));
const Log = lazy(() => import("@/views/Setting/Log"));
const History = lazy(() => import("@/views/Setting/History"));
const Order = lazy(() => import("@/views/Setting/Order"));
const Program = lazy(() => import("@/views/Setting/Program"));
const Alert = lazy(() => import("@/views/Setting/Alert"));
const Device = lazy(() => import("@/views/Device"));
const NotFound = lazy(() => import("@/components/NotFound"));
const Login = lazy(() => import("@/views/Login"));

const routes: RouteObject[] = [
  {
    path: "/",
    element: <HeaderLayout />,
    children: [
      {
        index: true,
        element: <Navigate to="/device" replace />,
      },
      {
        element: <AsideLayout />,
        children: [
          {
            path: "dashboard",
            element: <DashboardLayout />,
            children: [
              {
                path: ":id",
                element: <Dashboard />,
                children: [
                  {
                    index: true,
                    element: <Overview />,
                  },
                ],
              },
            ],
          },
          {
            path: "setting",
            element: <SettingLayout />,
            children: [
              {
                path: "log",
                handle: { title: "日志", admin: true },
                element: <Log />,
              },
              {
                path: "history",
                handle: { title: "历史数据" },
                element: <History />,
              },
              {
                path: "order",
                handle: { title: "订单" },
                element: <Order />,
              },
              {
                path: "program",
                handle: { title: "程序库" },
                element: <Program />,
              },
              {
                path: "alert",
                handle: { title: "限值警报" },
                element: <Alert />,
              },
            ],
          },
        ],
      },
      {
        path: "device",
        handle: { title: "设备列表" },
        element: <Device />,
      },
    ],
  },
  {
    path: "login",
    element: <Login />,
  },
  {
    path: "*",
    element: <NotFound />,
  },
];

export default __DEV__
  ? createBrowserRouter(routes, { basename: "/" })
  : createHashRouter(routes, { basename: "/" });
